package com.syxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.service.impl.CrudServiceImpl;
import com.syxd.common.utils.BeanHelper;
import com.syxd.common.utils.PageData;
import com.syxd.dao.SysSunCreditContactDao;
import com.syxd.dao.SysSunCreditDao;
import com.syxd.dto.SysSunCreditDto;
import com.syxd.entity.SysSunCreditContactEntity;
import com.syxd.entity.SysSunCreditEntity;
import com.syxd.service.SysSunAuditService;
import com.syxd.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysSunAuditServiceImpl extends CrudServiceImpl<SysSunCreditDao, SysSunCreditEntity, SysSunCreditDto> implements SysSunAuditService {

    @Autowired
    SysSunCreditContactDao sysSunCreditContactDao;
    @Autowired
    SysUserService sysUserService;

    @Override
    public QueryWrapper<SysSunCreditEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<SysSunCreditEntity> wrapper = new QueryWrapper<>();


        String branchName = (String) params.get("branchName");
        String householderIdcard = (String) params.get("householderIdcard");
        String householderPhone = (String) params.get("householderPhone");
        String spouseName = (String) params.get("spouseName");
        String spouseIdcard = (String) params.get("spouseIdcard");
        String spousePhone = (String) params.get("spousePhone");
        String householderName = (String) params.get("householderName");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        if (StringUtils.isNotBlank(endDate)) {
            endDate = endDate + " 23:59:59";
        }
        String orderType = (String) params.get("orderType");

        wrapper.eq(StringUtils.isNotBlank(branchName), "branch_name", branchName);
        wrapper.eq(StringUtils.isNotBlank(householderIdcard), "householder_idcard", householderIdcard);
        wrapper.eq(StringUtils.isNotBlank(householderPhone), "householder_phone", householderPhone);
        wrapper.eq(StringUtils.isNotBlank(spouseName), "spouse_name", spouseName);
        wrapper.eq(StringUtils.isNotBlank(spouseIdcard), "spouse_idcard", spouseIdcard);
        wrapper.eq(StringUtils.isNotBlank(spousePhone), "spouse_phone", spousePhone);
        wrapper.eq(StringUtils.isNotBlank(householderName), "householder_name", householderName);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_time", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_time", endDate);
        wrapper.eq("is_delete", Constant.FDELETE);
        if (params.get("activityIds") != null) {
            String activityIds = (String) params.get("activityIds");
            String[] split = activityIds.split(",");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                wrapper.like("activity_id", s);
                if (split.length - 1 != i) {
                    wrapper.or();
                }
            }
        }
        return wrapper;
    }

//    /**
//     * 阳光信贷需要审核分页数据
//     *
//     * @param params
//     * @return
//     */
//    @Override
//    public PageData<SysSunCreditDto> page(Map<String, Object> params) {
//        IPage<SysSunCreditEntity> page = getPage(params, "create_time", false);
//        String endDate = (String) params.get("endDate");
//        if (StringUtils.isNotBlank(endDate)) {
//            endDate = endDate + " 23:59:59";
//            params.put("endDate", endDate);
//        }
//        String startDate = (String) params.get("startDate");
//        if (StringUtils.isNotBlank(startDate)) {
//            startDate = startDate + " 00:00:00";
//            params.put("startDate", startDate);
//        }
//        List<SysSunCreditEntity> productInfoDTOS = baseDao.selectSunCreditPageInfo(params);
//        List<SysSunCreditDto> sysSunCreditDtos = BeanHelper.copyWithCollection(productInfoDTOS, SysSunCreditDto.class);
//        return new PageData<>(sysSunCreditDtos, page.getTotal());
//    }

    @Override
    public WebResult deleteSunCreditById(Long id, HttpServletRequest request) {

        SysSunCreditContactEntity sysSunCreditContactEntity = sysSunCreditContactDao.selectById(id);
        SysSunCreditEntity sysSunCreditEntity = new SysSunCreditEntity();
        sysSunCreditEntity.setId(sysSunCreditContactEntity.getSourceDataId().longValue());
        sysSunCreditEntity.setIsDelete(Constant.DELETE);
        sysSunCreditEntity.setUpdateTime(new Date());

        baseDao.updateById(sysSunCreditEntity);
        sysSunCreditContactEntity.setUpdateTime(new Date());
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setApproverId(userId);
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int i = sysSunCreditContactDao.updateById(sysSunCreditContactEntity);
        if (i > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    /**
     * 新增
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    public WebResult add(Long id, HttpServletRequest request) {
        SysSunCreditContactEntity sysSunCreditContactEntity = sysSunCreditContactDao.selectById(id);

        sysSunCreditContactEntity.setId(null);
        SysSunCreditEntity sysSunCreditEntity = BeanHelper.copyProperties(sysSunCreditContactEntity, SysSunCreditEntity.class);
        int insert = baseDao.insert(sysSunCreditEntity);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setId(id);
        sysSunCreditContactEntity.setApproverId(userId);
        sysSunCreditContactEntity.setUpdateTime(new Date());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int update = sysSunCreditContactDao.updateById(sysSunCreditContactEntity);
        if (insert > 0 && update > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    /**
     * 修改
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    public WebResult put(Integer id, HttpServletRequest request) {
        SysSunCreditContactEntity sysSunCreditContactEntity = sysSunCreditContactDao.selectById(id);
        SysSunCreditEntity sysSunCreditEntity = BeanHelper.copyProperties(sysSunCreditContactEntity, SysSunCreditEntity.class);
        sysSunCreditEntity.setId(sysSunCreditContactEntity.getSourceDataId().longValue());
        sysSunCreditEntity.setUpdateTime(new Date());
        int res = baseDao.updateById(sysSunCreditEntity);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setApproverId(userId);
        sysSunCreditContactEntity.setUpdateTime(new Date());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.pass.getValue());
        int update = sysSunCreditContactDao.updateById(sysSunCreditContactEntity);
        if (res > 0 && update > 0) {
            return new WebResult();
        }
        return new WebResult().error();
    }

    @Override
    public WebResult refuse(Integer id, HttpServletRequest request) {
        SysSunCreditContactEntity sysSunCreditContactEntity = sysSunCreditContactDao.selectById(id);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request, "userId").getData());
        sysSunCreditContactEntity.setApproverId(userId);
        sysSunCreditContactEntity.setUpdateTime(new Date());
        sysSunCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.reject.getValue());
        int update = sysSunCreditContactDao.updateById(sysSunCreditContactEntity);
        if (update > 0) {
            return new WebResult();
        }
        return new WebResult().error();

    }


}
